package com.shahe.GirlsDay.MyView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shahe.GirlsDay.R;
import com.shahe.GirlsDay.util.StaticClass;

import java.util.Calendar;
import java.util.Date;

public class WeightChartView extends View {

    private Paint paint;
    //private Calendar calendar;
    //private Date date;
    private String weight[] = new String[5];
    private TextView textView;
    String weightNum[][] = new String [5][2];
    int yValue[] = new int[5];
    int xValue[] = {200,300,400,500,600};

    public WeightChartView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    public void onDraw(Canvas canvas) {
        //canvas.drawColor(Color.WHITE);
        drawAxis(canvas);
        drawChart(canvas);
    }

    public void drawAxis(Canvas canvas) {
        paint.setColor(getResources().getColor(R.color.chartLine));
        paint.setStrokeWidth(8);
        canvas.drawLine(100, 600, 680, 600, paint);// 横线
        canvas.drawLine(100, 80, 100, 600, paint);// 竖线

        paint.setColor(getResources().getColor(R.color.chartDottedLine));
        paint.setStrokeWidth(4);
        canvas.drawLine(100, 500, 680, 500, paint);// 横虚线
        canvas.drawLine(100, 400, 680, 400, paint);// 横虚线
        canvas.drawLine(100, 300, 680, 300, paint);// 横虚线
        canvas.drawLine(100, 200, 680, 200, paint);// 横虚线
        canvas.drawLine(100, 100, 680, 100, paint);// 横虚线

        canvas.drawLine(200, 80, 200, 600, paint);// 竖虚线
        canvas.drawLine(300, 80, 300, 600, paint);// 竖虚线
        canvas.drawLine(400, 80, 400, 600, paint);// 竖虚线
        canvas.drawLine(500, 80, 500, 600, paint);
        canvas.drawLine(600, 80, 600, 600, paint);// 竖虚线

        int x = 180;
        int y = 610;

        paint.setTextSize(30);
        paint.setColor(Color.BLACK);

        //String month = calendar.get(Calendar.MONTH)+1+"";
        //String day = calendar.get(Calendar.DATE)+"";

        //Toast.makeText(getActivity(), day, Toast.LENGTH_SHORT).show();
        
        
        for (int i = 0; i < 5; i++) {
            // 横线
            canvas.drawText(StaticClass.weightDate[i], x, 640, paint);
            x += 100;
        }
        for (int i = 0; i < 6; i++) {
            // 竖线
            canvas.drawText(20 * (i + 0) + "", 30, y, paint);
            y -= 100;
        }
        canvas.drawText("kg",40,60,paint);
        canvas.drawText("Date",660,640,paint);


    }

    // 画曲线
    void drawChart(Canvas canvas) {
        paint.setColor(getResources().getColor(R.color.title_color));
        paint.setStrokeWidth(10);

        for (int i = 0; i < weight.length; i++){
            weightNum[i] = String.valueOf(Float.parseFloat(weight[i])*0.1).split(".");
            yValue[i] = 600 - Integer.valueOf(weightNum[i][0])*50 + Integer.valueOf(weightNum[i][1])*100;

            canvas.drawLine(yValue[i], xValue[i], yValue[i+1], xValue[i+1], paint);
        }
        //canvas.drawLine(100, 600, 200, 500, paint);// 横+50,竖-50；
        //canvas.drawLine(200, 500, 300, 400, paint);
       // canvas.drawLine(300, 400, 400, 500, paint);
        //canvas.drawLine(400, 500, 500, 400, paint);
    }

    public void getHLWeight() {
        float max = Float.parseFloat(weight[0]);
        float min = Float.parseFloat(weight[0]);
        setWeights();

        for (int i = 0; i < 5; i++) {

            if (max < Float.parseFloat(weight[i])) {
                max = Float.parseFloat(weight[i]);
            }
        }
        textView = (TextView) findViewById(R.id.tv_chart_l1);
        textView.setText(max + "");

        for (int i = 0; i < 5; i++) {

            if (min > Float.parseFloat(weight[i])) {
                min = Float.parseFloat(weight[i]);
            }
        }
        textView = (TextView) findViewById(R.id.tv_chart_r1);
        textView.setText(min + "");
    }

    public void setWeights() {
        if (StaticClass.dayList.size() >= 5) {
            for (int i = StaticClass.dayList.size() - 5; i < StaticClass.dayList
                    .size(); i++) {
                int j = 0;
                int z = 0;
                StaticClass.weightDate[j] = StaticClass.DayInfoList.get(i)
                        + "";
                weight[z] = StaticClass.DayInfoList.get(i)
                        .getWeight();
                j++;
                z++;

            }
        } else {
            for (int i = 0; i < StaticClass.dayList.size(); i++) {
                int j = 0;
                int z = 0;
                StaticClass.weightDate[j] = StaticClass.dayList.get(i)
                        + "";
                weight[z] = StaticClass.DayInfoList.get(i)
                        .getWeight();
                j++;
                z++;
            }
        }
    }

}
